home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Unix / ps2epsmac1.3.shar / ps-pict2macb.c < prev    next >
C/C++ Source or Header  |  1993-05-21  |  8KB  |  250 lines

  1. /*  ps-pict2macbin.c          george jefferson george@mech.seas.upenn.edu  */
  2. /*  initial release 4/4/92                                                 */
  3. /*  usage ps-pict2macbin file [-type TYPE ] [ -creator CREATOR ]           */
  4. /*  combines "file.ps" and "file.pict" to form "file.bin"                  */
  5. /*  file.bin is a macbinary format file, with macintosh name "file"        */
  6. /*  and creation/modification dates set to the current time of             */
  7. /*  day ( anybody care? )                                                  */
  8. /*  if type and creator are not specified, they are set to EPSF and MSWD   */
  9. /*  because _I_ use word5                                                  */
  10. /*  I see no reason to provide options to change the file names,           */
  11. /*  but it could easily be done                                            */
  12. /*  "file.ps" is taken to be the data fork, no assumptions are made here   */
  13. /*  about its format, however it is presumably postscript code -- It seems */
  14. /*  to be necessary to convert line endings to macintosh endings ( ^M )    */
  15. /*  I think it more appropriate to do that conversion externally ( eg tr ) */
  16. /*                                                                         */
  17. /*  "file.pict" is a pict data file, which is massaged into the resource   */
  18. /*  fork  Its format _is_ important here.                                  */
  19. /*  ppmtopict output is assumed, which ( for no apparent reason ) contains */
  20. /*  lots of null bytes prior to the actual data..                          */
  21. /*  ( in other words this program will not work with other types of        */
  22. /*  resources  )                                                           */
  23. /*  the format of the pict resource fork can be found in IM 1, page 128-131*/
  24. /*  many assumptions are 'hard wired' into this program -- eg we have      */
  25. /*  exactly one resource, of type PICT                                     */
  26. /* */
  27. /* It will be painfully obvious that this is the work of a novice C        */
  28. /* programmer, in fact the program is originally done in FORTRAN and (hand)*/
  29. /* converted ( note the lack of struct's and such )                        */
  30. /* anyway, it works on several sun4-ish machines around here, your milage  */
  31. /* may vary */
  32.  
  33.  
  34. #include <stdio.h>
  35. #include <sys/types.h>
  36. #ifdef notimeb
  37. #else
  38. #include <sys/timeb.h>
  39. #endif
  40.  
  41.  
  42. main (argc,argv)
  43.  int argc; char **argv;
  44. {
  45. int i,j,ksize;
  46. unsigned long mtim=0,ctim=0,unixtime,sizef,realsizef;
  47. int ireslength,idatalength,ipad;
  48. FILE *pic,*bin, *ps;
  49. char outname[30],inname[30],word[4],byte[1],zero[1];
  50. char ftyp[4];
  51. char fcrea[4];
  52. char macname[63], basename[30], psname[30], bbuf[128];
  53. extern unsigned long time2mac();
  54. if( argc <= 1 )
  55.   {
  56.     fprintf(stderr,
  57.     "usage ps-pict2macbin.c file.ps [ -type TYPE ][-creator CRTR ]\n");
  58.     exit();
  59.   }
  60. strcpy(basename,argv[1]);
  61. strcpy(inname,basename);strcat(inname,".pict");
  62. strcpy(outname,basename);strcat(outname,".bin");
  63. bzero(macname,63);
  64. strcpy(macname,basename);
  65. strcpy(psname,basename);strcat(psname,".ps");
  66. fprintf(stderr,"opening pict file %s \n",inname);
  67. if( (pic=fopen(inname,"r")) == NULL )
  68.   {fprintf(stderr,"error opening file\n");exit();}
  69. fprintf(stderr,"opening output file %s \n",outname);
  70. if( (bin=fopen(outname,"w")) == NULL )
  71.   {fprintf(stderr,"error opening file\n");exit();}
  72. fprintf(stderr,"opening ps file %s \n",psname);
  73. if( (ps=fopen(psname,"r")) == NULL )
  74.   {fprintf(stderr,"error opening file\n");exit();}
  75. for(i=1;i<=128;i++)write_one(bin,0); /*leave room for info */
  76. /* copy ps file over to binary */
  77.    idatalength=0;i=0;
  78.    while (fread(byte, sizeof(*byte), 1, ps) > 0)
  79.      {
  80.       if(++i==129)i=1;
  81.       ++idatalength;
  82.       fwrite(byte, sizeof(*bbuf), 1, bin);
  83.      }
  84.    fclose(ps);
  85. for(i=i;i<128;i++)write_one(bin,0); /*pad to next 128 byte boundary */
  86. /* write the pict resource from the pict data file */
  87. fread(byte,sizeof(*byte),1,pic);i=1;
  88. while( strlen(byte)==0 ){fread(byte,sizeof(*byte),1,pic);i++;}
  89. ksize=256*ibyte(byte);
  90. fread(byte,sizeof(*byte),1,pic);
  91. ksize=ksize+ibyte(byte); /* length of actual resource data */
  92. sizef=ftell(pic);fseek(pic,0l,2);realsizef=ftell(pic)-i+1;fseek(pic,sizef,0);
  93. if(ksize != realsizef)fprintf(stderr,"real length %i not reported length %i fixing...\n",realsizef,ksize);
  94. ireslength=256+realsizef+4+50; /* total length of resource fork */
  95. write_header(bin,realsizef);
  96. for(i=1;i<=112;i++)write_one(bin,0);
  97. for(i=1;i<=128;i++)write_one(bin,0); /*reserver stuff */
  98. write_four(bin,realsizef); /* length of resource data */
  99. write_two(bin,ksize); /* length of resource data */
  100. for( j=1;j<=realsizef-2;j++){
  101. fread(byte,sizeof(*byte),1,pic);
  102. fwrite(byte,sizeof(*byte),1,bin);
  103. }
  104. /*    resource map    */
  105. write_header(bin,realsizef);/* copy of header */
  106.   write_four(bin,0);write_two(bin,0); /* 6 null bytes ( reserved ) */
  107.   write_two(bin,0); /* attributes*/
  108.   write_two(bin,28); /* offset from beginning of map to type list */
  109.   write_two(bin,28+22); /* beg map to name list */
  110. /* type list */
  111.   write_two(bin,0); /* only one resource type */
  112.   strcpy(word,"PICT");
  113.   fwrite(word,sizeof(word),1,bin);/* resource type */
  114.   write_two(bin,0); /* only one pict */
  115.   write_two(bin,10); /* begining of type list to reference list */
  116. /* reference list */
  117. write_two(bin,256); /* resource id */
  118. write_one(bin,-1);write_one(bin,-1); /* no name for resource */
  119. write_one(bin,0); /* attributes */
  120. write_two(bin,0);write_one(bin,0) ; /* 3 byte offset within resourc */
  121. write_four(bin,0); /* reserved */
  122. /* pad file to nearest 128 byte boundary */
  123. ipad=(ireslength/128+1)*128-ireslength;
  124. for(i=1;i<=ipad;i++)write_one(bin,0); 
  125. /* this is the end of the resource fork, and the end of the file */
  126. /* macbinary info header */
  127. fseek(bin,0l,0);/* rewind so that we can write the info header */
  128. write_one(bin,0); /* zero */
  129. write_one(bin,strlen(macname)); /* length of file name */
  130. fwrite(macname,sizeof(macname),1,bin); /* name padded to 63 bytes */
  131. bcopy("MSWD",fcrea,4);
  132. bcopy("EPSF",ftyp,4);
  133. for( i=1;i<argc;i++ )
  134.   {
  135.     if( strcmp(argv[i],"-type") == 0 )bcopy(argv[++i],ftyp,4);
  136.     if( strcmp(argv[i],"-creator") == 0 )bcopy(argv[++i],fcrea,4);
  137.   }
  138. fwrite(ftyp,sizeof(ftyp),1,bin);/* file type */
  139. fwrite(fcrea,sizeof(fcrea),1,bin);/* file creator */
  140. write_one(bin,1); /* should this be a zero? */
  141. write_one(bin,0);
  142. write_four(bin,0);write_two(bin,0);
  143. write_one(bin,0);
  144. write_one(bin,0);
  145. write_four(bin,idatalength); /* length og data file */
  146. write_four(bin,ireslength); /* length of resource file */
  147. unixtime = time(&unixtime);
  148. ctim=time2mac(unixtime);
  149. write_four(bin,ctim);
  150. write_four(bin,mtim);
  151. /* all done */
  152. }
  153.  
  154. write_header(iu,kbytes)
  155. FILE *iu;int kbytes;
  156. {
  157.   write_four(iu,256);/*c     4 bytes -> length of header+system+application, always 16+112+128=256*/
  158. /*c next four bytes -> length of pict data + header + 4 bytes for size*/
  159.       write_four(iu,256+kbytes+4);
  160. /*c next four -> length of data*/
  161.       write_four(iu,kbytes+4);
  162. /*c next four -> length of map ( always same )*/
  163.       write_four(iu,50);
  164. }
  165.  
  166. write_one(iu,k)
  167. int k ; FILE *iu ;
  168. {
  169.   unsigned char byte[1];
  170.   byte[0]=k;
  171.   fwrite(byte,sizeof(*byte),1,iu);
  172. }
  173.  
  174.  
  175.  
  176.  
  177. ibyte( b )
  178.      char *b;
  179. {
  180.   int ibyte;
  181.   ibyte=b[0];
  182.   if(ibyte < 0 )ibyte=ibyte+256;
  183.   return(ibyte);
  184. }
  185.  
  186.  
  187. write_four(iu,k)
  188. unsigned long k ; FILE *iu ;
  189. {
  190.   int k1,k2,l,n[4];
  191.   unsigned char byte[1];
  192.   n[0]=16777216;n[1]=65536;n[2]=256;n[3]=1;
  193.   k1=0;
  194.   for(l=0;l<4;l++)
  195.     {
  196.       k2=(k-k1)/n[l];
  197.       byte[0]=k2;
  198.       fwrite(byte,sizeof(*byte),1,iu);
  199.       k1=k1+k2*n[l];
  200.     }
  201. }
  202.  
  203. write_two(iu,k)
  204.      int k ; FILE *iu ;
  205. {
  206.   int k1,k2,l,n[2];
  207.   unsigned char byte[1];
  208.   n[0]=256;n[1]=1;
  209.   k1=0;
  210.   for(l=0;l<2;l++)
  211.     {
  212.       k2=(k-k1)/n[l];
  213.       byte[0]=k2;
  214.       fwrite(byte,sizeof(*byte),1,iu);
  215.       k1=k1+k2*n[l];
  216.     }
  217. }
  218.  
  219.  
  220. #ifndef sysv
  221. typedef unsigned long ulong;    /* 4 bytes */ 
  222. #endif
  223.  
  224.  
  225. /* 'borrowed' from mcvert */
  226. /* Convert Unix time (GMT since 1-1-1970) to Mac
  227.                                     time (local since 1-1-1904) */
  228. #define MACTIMEDIFF 0x7c25b080 /* Mac time of 00:00:00 GMT, Jan 1, 1970 */
  229.  
  230. /* Compatibility issues */
  231. #ifdef BSD
  232. #define long2mac (ulong) htonl
  233. #else
  234. #define long2mac
  235. #endif
  236.  
  237. ulong time2mac(time)
  238. ulong time;
  239. {
  240. #ifdef notimeb
  241.   return long2mac( time + MACTIMEDIFF );
  242. #else
  243.   struct timeb tp;
  244.   ftime(&tp);
  245.   return long2mac(time + MACTIMEDIFF
  246.           - 60 * (tp.timezone /*- 60 * tp.dstflag*/));
  247. #endif
  248. }
  249. /* correcting for daylight savings time needs to be improved. */
  250.